import axios from 'axios'
import qs from 'qs'
import { URL } from '@/config'
import { IHttpOptions } from '@/typing'

export default class HTTP {
  axiosPost(options: IHttpOptions) {
    axios({
      url: URL.API_BASE_URL + options.url,
      method: 'post',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: qs.stringify(options.data)
    })
      .then((res) => options.success(res.data))
      .catch((err) => options.error(err))
  }

  axiosPostFile(options: IHttpOptions) {
    axios({
      url: URL.API_BASE_URL + options.url,
      method: 'post',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data: options.data
    })
      .then((res) => options.success(res.data))
      .catch((err) => options.error(err))
  }

  axiosGet(options: IHttpOptions) {
    axios(URL.API_BASE_URL + options.url)
      .then((res) => options.success(res.data))
      .catch((err) => options.error(err))
  }

  axiosDelete(options: IHttpOptions) {
    axios({
      url: URL.API_BASE_URL + options.url,
      method: 'delete',
      params: options.data
    })
      .then((res) => options.success(res.data))
      .catch((err) => options.error(err))
  }

  axiosPut(options: IHttpOptions) {
    axios({
      url: URL.API_BASE_URL + options.url,
      method: 'put',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: qs.stringify(options.data)
    })
      .then((res) => options.success(res.data))
      .catch((err) => options.error(err))
  }
}
